<template>
  <div class="app-container">
    <el-container>
      <el-card width="200px"><!-- 产品列表 -->
        产品栏
        <el-table
          :data="list"
          stripe
          border
          highlight-current-row
          @current-change="handleCurrentChange"
          style="width:180px;margin-top: 10px;scrollbar-width: none"
          ref="productTable">
          <el-table-column prop="name" label="产品名称" width="180"/>
        </el-table>
      </el-card>
      <el-main>
        产品规格
        <div class="search-div" style="margin-top: 10px;">
          <el-form label-width="70px" size="small">
            <el-row>
              <el-col :span="8">
                <el-form-item label="关 键 字">
                  <el-input style="width: 95%" v-model="searchObj.keyword" placeholder="产品名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="操作时间">
                  <el-date-picker
                    v-model="createTimes"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    style="margin-right: 10px;width: 100%;"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row style="display:flex">
              <el-button type="primary" icon="el-icon-search" size="mini"  @click="fetchData()">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
            </el-row>
          </el-form>
        </div>

        <!-- 工具条 -->
        <div class="tools-div">
          <el-button type="success"  icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
        </div>
        <!-- 列表 -->
        <el-table
          v-loading="listLoading"
          :data="specList"
          stripe
          border
          style="width: 100%;margin-top: 10px;">

          <el-table-column
            label="序号"
            width="70"
            align="center">
            <template slot-scope="scope">
              {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
          </el-table-column>

          <el-table-column prop="name" label="规格名称" />
          <el-table-column prop="code" label="条码"/>
          <el-table-column prop="color" label="颜色"/>
          <el-table-column prop="volume" label="体积" />
          <el-table-column prop="productionTime" label="生产时间" />
          <el-table-column prop="limitTime" label="保质期" />
          <el-table-column prop="basicPrice" label="成本价" />
          <el-table-column prop="salePrice" label="售价" />
          <el-table-column prop="createTime" label="创建时间" />
          <el-table-column prop="updateTime" label="更新时间" />

          <el-table-column label="操作"  align="center" fixed="right">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除" />
            </template>
          </el-table-column>
        </el-table>


        <el-dialog title="添加/修改" :visible.sync="dialogVisible"
                   @open="typeDialogList"
                   width="40%" >
          <el-form ref="dataForm" :model="sysUser"  label-width="100px" size="small" style="padding-right: 40px;">

            <el-form-item label="规格名称" prop="name">
              <el-input v-model="sysUser.name"/>
            </el-form-item>
            <el-form-item label="条码" prop="code">
              <el-input v-model="sysUser.code"/>
            </el-form-item>
            <el-form-item label="颜色" prop="color">
              <el-input v-model="sysUser.color"/>
            </el-form-item>
            <el-form-item label="体积" prop="volume">
              <el-input v-model="sysUser.volume"/>
            </el-form-item>
            <el-form-item label="成本价" prop="basicPrice">
              <el-input v-model="sysUser.basicPrice"/>
            </el-form-item>
            <el-form-item label="售价" prop="salePrice">
              <el-input v-model="sysUser.salePrice"/>
            </el-form-item>
            <el-form-item label="生产时间" prop="productionTime">
              <el-date-picker
                v-model="sysUser.productionTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="请选择产品生产时间"
              />
            </el-form-item>
            <el-form-item label="到期时间" prop="limitTime">
              <el-date-picker
                v-model="sysUser.limitTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="请选择产品到期时间"
              />
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import api from '@/api/basic/productSpec'
import productApi from '@/api/basic/product'

export default {
  data() {
    return {
      listLoading: false, // 数据是否正在加载
      list: null, // 列表
      specList: null, // 列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      // 其他数据...
      currentRow: null, // 存储当前选中的行

      createTimes: [],

      dialogVisible: false,
      sysUser: {},

      dialogRoleVisible: false,
      allRoles: [], // 所有角色列表
      userRoleIds: [], // 用户的角色ID的列表
      isIndeterminate: false, // 是否是不确定的
      checkAll: false // 是否全选
    }
  },
  created() {
    //调用列表方法
    this.fetchData()
  },
  methods:{

    //删除
    removeDataById(id) {
        this.$confirm('此操作将永久删除该产品分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //调用方法删除
          api.removeById(id)
            .then(response => {
              //提示
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //刷新
              this.fetchData()
          })
        })
    },
    //根据id查询，数据回显
    edit(id) {
      //弹出框
      this.dialogVisible = true
      //调用接口查询
      api.getUserId(id)
        .then(response => {
          this.sysUser = response.data
        })
    },
    //添加或者修改方法
    saveOrUpdate() {
      console.log(this.sysUser.id)
      if(!this.sysUser.id) {
        this.save()
      } else {
        this.update()
      }
    },
    //修改
    update() {
      api.update(this.sysUser)
        .then(response => {
          //提示
          this.$message.success('操作成功')
          //关闭弹框
          this.dialogVisible = false
          //刷新
          this.fetchData()
        })
    },
    //添加
    save() {
      api.save(this.sysUser)
        .then(response => {
          //提示
          this.$message.success('操作成功')
          //关闭弹框
          this.dialogVisible = false
          //刷新
          this.fetchData()
        })
    },
    //添加弹框的方法
    add() {
      this.dialogVisible = true
      this.sysUser = {}
    },
    // 处理选中行变化
    handleCurrentChange(val) {
      if (val) {
        console.log('选中行的数据:', val)
        this.specListDta(val.id)
        // 在这里执行你需要的响应事件
        // 例如，显示选中行的详细信息
        this.sysUser.productId = val.id
      } else {

        console.log('取消选中')
        this.sysUser = {}
      }
    },
    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },
    //列表
    fetchData(page=1) {
      productApi.getList()
        .then(response => {
          this.list = response.data
          if (this.list && this.list.length > 0) {
            this.$refs.productTable.setCurrentRow(this.list[0])
            this.currentRow = this.list[0]
            this.specListDta(this.list[0].id)
            // 例如，显示选中行的详细信息
            this.sysUser.productId = this.list[0].id
          }

        })

    },
    specListDta(page) {
      this.searchObj.productId = page
      if(this.createTimes && this.createTimes.length==2) {
        this.searchObj.createTimeBegin = this.createTimes[0]
        this.searchObj.createTimeEnd = this.createTimes[1]
      }
      api.getPageList(this.searchObj)
        .then(response => {
          this.specList = response.data
        })
    }
  }
}
</script>
